/* os_common.h : os common define
 * CopyRight (C) 2008 dgnbbs
 * drangon_zhou@21cn.com
 * 2008-10
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_OS_COMMON_H
#define INCLUDED_OS_COMMON_H

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdint.h>   /* TODO : WIN32 not have this */
#include <stddef.h>   /* NULL */

/* <1> log */
/* simple log, log to syslog, move this to osdep too, WIN32 not have this
 * NOTE : this is program log, not the business log
 */

#include <syslog.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>  /* getpid() */

#include <errno.h>
#include <assert.h>
#include <stdlib.h>

#ifndef APP_LOG_FACILITY
#define APP_LOG_FACILITY LOG_LOCAL3
#endif 

#define INNER_PRINT_LOG( syslog_level, fmt, args... ) \
	do { \
		struct timeval inner_tv; \
		gettimeofday( &inner_tv, NULL ); \
		syslog( APP_LOG_FACILITY | syslog_level, \
				"[%06d] PID %d [%s:%d:%s] "fmt, (int)inner_tv.tv_usec, getpid(), \
				__FILE__, __LINE__, __func__, ##args); \
	} while( 0 )

#define PR_ERR( fmt, args... ) INNER_PRINT_LOG( LOG_ERR, "[ERR] "fmt, ##args )
#define PR_WARN( fmt, args... ) INNER_PRINT_LOG( LOG_ERR, "[WARN] "fmt, ##args )
#define PR_INFO( fmt, args... ) INNER_PRINT_LOG( LOG_INFO, "[INFO] "fmt, ##args )
#define PR_DEBUG( fmt, args... ) INNER_PRINT_LOG( LOG_DEBUG, "[DEBUG] "fmt, ##args )

#define ASSERT	assert

/* <2> cpp wrap */
#ifdef __cplusplus
#define BEGIN_EXTERN_C_DECL extern "C" {
#define END_EXTERN_C_DECL }
#else
#define BEGIN_EXTERN_C_DECL 
#define END_EXTERN_C_DECL
#endif 


#endif /* INCLUDED_OS_COMMON_H */

